﻿using CommHelper;
using SDKs;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tea;

namespace NX5WinServies
{
    public class Lesson
    {
        public static void ChangeLessonStatus()
        {
            DataManager dm = new DataManager();
            DataTable dt = dm.Procedure("Cus_GetLessStatus24", "[]").table;
            Log.Write("Lesson", "找到" + dt.Rows.Count + "个课程");
            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    DateTime StartTime = DateTime.Parse(dr["StudyDay"].ToString() + " " + dr["StartTime"].ToString());
                    DateTime EndTime = DateTime.Parse(dr["StudyDay"].ToString() + " " + dr["EndTime"].ToString());
                    Int32 Status = Int32.Parse(dr["Status"].ToString());
                    if (Status == 2)
                    {
                        //判断开始时间，将已确认的改为进行中 ==> 4 
                        if (DateTime.Now.AddMinutes(3) > StartTime)
                        { 
                            Log.Write("Lesson", dr["ID"].ToString() + "即将设置为4");
                            dm.Procedure("Cus_ChangeLesStatus", "[{\"ID\":" + dr["ID"].ToString()
                                + ",\"Status\":4}]");
                        }
                    }
                    if (Status == 4)
                    {
                        //判断结束时间，将进行中的改为已经结束 ==> 5
                        if (EndTime.AddMinutes(3) < DateTime.Now)
                        {
                            Log.Write("Lesson", dr["ID"].ToString() + "即将设置为5");
                            dm.Procedure("Cus_ChangeLesStatus", "[{\"ID\":" + dr["ID"].ToString()
                                + ",\"Status\":5}]");
                        }
                    }
                }
                catch(Exception ex)
                {
                    Log.Write("Lesson", dr["StudyDay"].ToString() + " " + dr["StartTime"].ToString());
                }
            }
        }


        public static void SendMsg()
        {
            DataManager dm = new DataManager();
            DataTable dt = dm.Procedure("Sys_SmsGetLess", "[]").table;
            Log.Write("Lesson-SMS", "找到" + dt.Rows.Count + "个课程");
            Sms sms = new Sms();
            String SignName = ConfigManager.GetAppSettings("SignName");
            String TemplateCode = "";
            String PhoneNumbers = "";
            String TemplateParam = "";

            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    Int32 Status = Int32.Parse(dr["Status"].ToString());
                    switch (Status)
                    {
                        case 1:
                            //SMS_493415110
                            //学生${ student}预约了${ date}的${ lesstype}课程。https://nx5.abc5g.com/lscomfirm?uuid=${uuid}
                            TemplateCode = "SMS_493885352";
                            PhoneNumbers = dr["TeacherPhone"].ToString();
                            TemplateParam = "{\"title\":\"" + dr["Title"].ToString()
                                + "\",\"date\":\"" + dr["StudyDay"].ToString() + " " + dr["StartTime"].ToString()
                                + "\",\"lestype\":\"" + (dr["LessonType"].ToString() == "1" ? "学习课" : "保温课")
                                + "\",\"type\":\"" + dr["LessonType"].ToString()
                                + "\",\"status\":\"" + dr["Status"].ToString() +"\"}";
                            break;
                        case 2:
                            //SMS_493220130
                            //${teacher}确认了课程,将于${date}开始上课
                            TemplateCode = "SMS_493895327";
                            PhoneNumbers = dr["StudentPhone"].ToString();
                            TemplateParam = "{\"title\":\"" + dr["Title"].ToString()
                                + "\",\"date\":\"" + dr["StudyDay"].ToString() + " " + dr["StartTime"].ToString()
                                + "\",\"lestype\":\"" + (dr["LessonType"].ToString() == "1" ? "学习课" : "保温课")
                                + "\",\"type\":\"" + dr["LessonType"].ToString()
                                + "\",\"status\":\"" + dr["Status"].ToString() + "\"}";
                            break;
                        case 3:
                            //SMS_493195127
                            //${ teacher}取消了${ date}的课程
                            TemplateCode = "SMS_493800360";
                            PhoneNumbers = dr["TeacherPhone"].ToString();
                            TemplateParam = "{\"title\":\"" + dr["Title"].ToString()
                                + "\",\"date\":\"" + dr["StudyDay"].ToString() + " " + dr["StartTime"].ToString()
                                + "\",\"lestype\":\"" + (dr["LessonType"].ToString() == "1" ? "学习课" : "保温课")
                                + "\",\"type\":\"" + dr["LessonType"].ToString()
                                + "\",\"status\":\"" + dr["Status"].ToString() + "\"}";
                            break; 
                        case 5:
                            //SMS_493360143
                            //${date}的课程已经结束，可以通过https://nx5.abc5g.com/h5/pages/over?uid=${uuid}查看报告并评价
                            TemplateCode = "SMS_493860352";
                            PhoneNumbers = dr["StudentPhone"].ToString();
                            TemplateParam = "{\"title\":\"" + dr["Title"].ToString()
                                + "\",\"date\":\"" + dr["StudyDay"].ToString() + " " + dr["EndTime"].ToString()
                                + "\",\"lestype\":\"" + (dr["LessonType"].ToString() == "1" ? "学习课" : "保温课")
                                + "\",\"type\":\"" + dr["LessonType"].ToString()
                                + "\",\"status\":\"" + dr["Status"].ToString() + "\"}";
                            break;
                        default:
                            break;
                    }
                    if(PhoneNumbers == "")
                    {
                        continue;
                    }
                    
                    Log.Write("Lesson-SMS", "====当前状态:" + dr["StatusTitle"].ToString() + "====");
                    Log.Write("Lesson-SMS", "====" + TemplateCode + "====");
                    Log.Write("Lesson-SMS", "====" + PhoneNumbers + "====");
                    Log.Write("Lesson-SMS", "====" + TemplateParam + "====");
                    sms.SendSms(SignName, TemplateCode, PhoneNumbers, TemplateParam);
                    dm.SqlCmd("update Cus_Lesson set SendMsg = 1 where id = " + dr["ID"].ToString());
                }
                catch (Exception ex)
                {
                    Log.Write("Lesson-SMS", dr["ID"].ToString() + "==============>" + ex.ToString());
                }
            }
        }
    }
}
